import axios from 'axios';
import React, { FC, useEffect, useState } from 'react';
export const BASE_URL = process.env.PUBLIC_URL;
const AREA: string = 'AREA|88cff55c-aaa4-e2e0';

export const BASE_AREA = {
  area: AREA,
};
// type Result = <T extends keyof D>

/**
 *
 * @param url
 * @param data 接收一个{ param }
 * @param config
 * @returns
 * 主要职责 负责axios请求
 */
const useRequest = (url: string, data: any | null, config?: any | null) => {
  const [loading, setLoading] = useState(false);
  const [result, setResult] = useState(null);
  const [error, setError] = useState(null);

  const request = async () => {
    setLoading(true);
    try {
      const result = await axios({
        url: `${BASE_URL}${url}`,
        params: data,
        method: 'get',
      });
      if (result && result.status >= 200 && result.status <= 304) {
        setResult(result.data);
      } else {
        setError(new Error('get data error in useRequest'));
      }
    } catch (reason) {
      setError(reason);
    }
    setLoading(false);
  };
  useEffect(() => {
    request();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  return {
    loading,
    result,
    error,
  };
};

export default useRequest;
